package com.urtime.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.simple.JSONObject;

import com.urtime.dao.UserDAO;
import com.urtime.model.User;
import com.urtime.util.Common;
import com.urtime.util.ErrorJson;

/**
 * Servlet implementation class PostDispatcher
 */
public class PostDispatcher extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PostDispatcher() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ServletUtil.printError(request,response,ErrorJson.ERROR_UNKNOWN_METHOD,"GET");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String method = request.getParameter("method");
		if(method==null||method.length()==0){
			ServletUtil.printError(request,response,ErrorJson.ERROR_UNKNOWN_METHOD, null);
			return;
		}
		if("register".equals(method)){
			doReg(request,response);
		}else if("login".equals(method)){
			doLogin(request,response);
		}else{
			ServletUtil.printError(request,response,ErrorJson.ERROR_UNKNOWN_METHOD, method);
		}
	}
	
	
    private void doReg(HttpServletRequest request, HttpServletResponse response) throws IOException{
    	String un = request.getParameter("username");
    	String pw = request.getParameter("password");
    	int err = UserDAO.AddUser(un, pw);
    	ServletUtil.printError(request,response,err,null);
    }
    
    
	private void doLogin(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String un = request.getParameter("username");
		String pw = request.getParameter("password");
		if(UserDAO.validatePassword(pw) && UserDAO.validateUsername(un)){
			String m = Common.md5(pw);
			List<User> u = UserDAO.queryUser("where username='"+un+"' and password='"+m+"'");
			if(u.size()!=0){
        		HttpSession client=request.getSession(true);
        		client.setAttribute("user", u.get(0));
        		client.setAttribute("userid", u.get(0).getId());
        		JSONObject obj = ErrorJson.getErrorJson(ErrorJson.ERROR_OK,null);
        		obj.put("realName", u.get(0).getRealName());
        		response.setContentType("text/plain");
        		response.getWriter().print(obj);
        		response.flushBuffer();
			}else{
				ServletUtil.printError(request,response,ErrorJson.ERROR_USRENAME_PASSWORD,null);
			}
		}else{
			ServletUtil.printError(request,response,ErrorJson.ERROR_PARAMS,null);
		}
		
	}

}
